package com.buss.base.controller;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.base.model.Company;
import com.buss.base.service.CompanyService;
import com.buss.core.controller.BaseController;
import com.buss.core.dic.RecordStatus;
import com.buss.core.model.DataModel;
import com.buss.core.model.Sysmenu;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;

@Controller
@RequestMapping(value = "/root/company")
public class CompanyController extends BaseController {

	@Autowired
	private CompanyService companyService;
	
	@Override
	@RequestMapping("/page")
	public ModelAndView listPage(String menuid) {
		ModelAndView view = new ModelAndView("company/listpage");
		return view;
	}


	@RequestMapping("/{type}")
	public ModelAndView editPage(@PathVariable String type, String id) {		
		ModelAndView view = new ModelAndView("company/edit");
		Company company = new Company();
		if(EDIT_PAGE.equals(type)){
			company = companyService.loadCompany(id);
		}else{
			company.setStatus(RecordStatus.normal);
		}
		view.addObject("company",company);
		return view;
	}


	/**
	 * 新增公司/单位信息
	 * @param company
	 * @param validResult
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add( @Valid Company company,BindingResult validResult){
		companyService.saveCompany(company);
		return SysStaticParam.resultMessage("新增信息成功!");
	}
	
	/**
	 * 修改公司/单位信息
	 * @param company
	 * @param validresult
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String,Object> edit( @Valid Company company,BindingResult validresult){
		companyService.editCompany(company);		
		return SysStaticParam.resultMessage("修改信息成功!");
	}
	
	/**
	 * 删除多个公司/单位信息
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> deleteCompanys(String[] ids){
		companyService.removeCompany(ids);
		return SysStaticParam.resultMessage("删除公司/单位信息成功!");
	}
	
	/**
	 * 更改单位状态
	 * @param ids
	 * @return
	 */
	@RequestMapping("/updatestatus/{status}")
	@ResponseBody
	public Map<String,Object> updateStatus(@PathVariable RecordStatus status, String[] ids){
		companyService.updateStatus(ids, status);
		return SysStaticParam.resultMessage("更新公司/单位状态成功!");
	}
	
	/**
	 * 获取公司/单位信息列表
	 * @param name
	 * @param pinyin
	 * @param address
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list( String name, String pinyin, String address, Pagination page){
		ResultList<Company> resultList = companyService.queryCompany(name, pinyin, address, page);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(SysStaticParam._RESULT, resultList);
		map.put(SysStaticParam._MESSAGE, SysStaticParam.resultMessageSuccess("获取公司/单位信息列表成功!"));		
		return map;
	}
	
	@Override
	public ModelAndView editPage(String type, Long id) {
		return null;
	}
	
	@Override
	public Map<String, Object> delete(Long[] ids) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
